Load the libraries

library(tidyverse)
library(tidytuesdayR)
library(here)
library(tidyr)
library(ggmap)
library(maps)
library(mapdata)
library(mapproj)
library(plotly)

Read in the data

sf_rent <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2022/2022-07-05/rent.csv')
glimpse(sf_rent)
## Rows: 200,796
## Columns: 17
## $ post_id     <chr> "pre2013_134138", "pre2013_135669", "pre2013_127127", "pre…
## $ date        <dbl> 20050111, 20050126, 20041017, 20120601, 20041021, 20060411…
## $ year        <dbl> 2005, 2005, 2004, 2012, 2004, 2006, 2007, 2017, 2009, 2006…
## $ nhood       <chr> "alameda", "alameda", "alameda", "alameda", "alameda", "al…
## $ city        <chr> "alameda", "alameda", "alameda", "alameda", "alameda", "al…
## $ county      <chr> "alameda", "alameda", "alameda", "alameda", "alameda", "al…
## $ price       <dbl> 1250, 1295, 1100, 1425, 890, 825, 1500, 2925, 450, 1395, 1…
## $ beds        <dbl> 2, 2, 2, 1, 1, 1, 1, 3, NA, 2, 2, 5, 4, 0, 4, 1, 3, 3, 1, …
## $ baths       <dbl> 2, NA, NA, NA, NA, NA, 1, NA, 1, NA, NA, NA, 3, NA, NA, NA…
## $ sqft        <dbl> NA, NA, NA, 735, NA, NA, NA, NA, NA, NA, NA, 2581, 1756, N…
## $ room_in_apt <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0…
## $ address     <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA…
## $ lat         <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 37.53494, …
## $ lon         <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA…
## $ title       <chr> "$1250 / 2br - 2BR/2BA   1145 ALAMEDA DE LAS PULGAS", "$12…
## $ descr       <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA…
## $ details     <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, "<p class=…
head(sf_rent)
## # A tibble: 6 × 17
##   post_id          date  year nhood city  county price  beds baths  sqft room_…¹
##   <chr>           <dbl> <dbl> <chr> <chr> <chr>  <dbl> <dbl> <dbl> <dbl>   <dbl>
## 1 pre2013_134138 2.01e7  2005 alam… alam… alame…  1250     2     2    NA       0
## 2 pre2013_135669 2.01e7  2005 alam… alam… alame…  1295     2    NA    NA       0
## 3 pre2013_127127 2.00e7  2004 alam… alam… alame…  1100     2    NA    NA       0
## 4 pre2013_68671  2.01e7  2012 alam… alam… alame…  1425     1    NA   735       0
## 5 pre2013_127580 2.00e7  2004 alam… alam… alame…   890     1    NA    NA       0
## 6 pre2013_152345 2.01e7  2006 alam… alam… alame…   825     1    NA    NA       0
## # … with 6 more variables: address <chr>, lat <dbl>, lon <dbl>, title <chr>,
## #   descr <chr>, details <chr>, and abbreviated variable name ¹​room_in_apt
# Get data for counties and filter to keep just the counties in California
CA<-map_data("county") %>%
  filter(region == "california")
head(CA)
##        long      lat group order     region subregion
## 1 -121.4785 37.48290   157  6965 california   alameda
## 2 -121.5129 37.48290   157  6966 california   alameda
## 3 -121.8853 37.48290   157  6967 california   alameda
## 4 -121.8968 37.46571   157  6968 california   alameda
## 5 -121.9254 37.45998   157  6969 california   alameda
## 6 -121.9483 37.47717   157  6970 california   alameda

Clean the data

clean_rent <- sf_rent %>% # creates a new df, clean rent
  drop_na() %>% # removes empty values
  group_by(city) # groups by city
head(clean_rent) # shows the data
## # A tibble: 6 × 17
## # Groups:   city [1]
##   post_id        date  year nhood   city  county price  beds baths  sqft room_…¹
##   <chr>         <dbl> <dbl> <chr>   <chr> <chr>  <dbl> <dbl> <dbl> <dbl>   <dbl>
## 1 4710888130 20141012  2014 alameda alam… alame…  2250     2     1  1080       0
## 2 4988581576 20150421  2015 alameda alam… alame…  2650     2     1   950       0
## 3 4988561264 20150421  2015 alameda alam… alame…  1950     2     1   800       0
## 4 4855533017 20150120  2015 alameda alam… alame…  2650     2     1   950       0
## 5 4631188738 20140824  2014 alameda alam… alame…  3295     4     1  1716       0
## 6 4794051100 20141209  2014 alameda alam… alame…  1860     1     1   705       0
## # … with 6 more variables: address <chr>, lat <dbl>, lon <dbl>, title <chr>,
## #   descr <chr>, details <chr>, and abbreviated variable name ¹​room_in_apt

Create the map

# Get base map
# base_map <- get_map(location = c(lon = -122, lat = 37), zoom = 15, maptype = "satellite")


p <- ggplot() + # labels p as the CA map
  geom_polygon(data = CA, # geom to plot CA
               aes(x = long, # assigning axes
                   y = lat, 
                   group = group, 
                   fill = region,
                   show.legend = FALSE),
               color = "black")+ # adding color to the lines
  geom_point(data = clean_rent, # adds another geom to plot the rent/prices
             aes(x = lon, y = lat, color = price), # assigns axes and adjusts density, size
                 alpha = 1,
                 size = 1,
                 show.legend = TRUE) + # keeps the legend
  ylim(36, 39.5) + # assigns specific values for y axis label to focus on the SF area
  # xlim(-120.8, -123)
  labs(title = "Rents in San Francisco")
    
    ggplotly(p, showlegend = FALSE) # makes the plot interactive and removes legend

Save the map

ggsave(here("TT_08","Output","SF rent.png"),
       width = 6, height = 6) # in inches)
I could not figure out how to get rid of lat and long from the label while hovering. I wanted to show only the prices on map. Butttt it does serve the purpose!